//打包 二维的01背包
#include <iostream>
using namespace std;
const int MAXN=380;
const int MAXV=380;
const int MAXG=380;
int v,g,n;
int ti[MAXN+1],vi[MAXN+1],gi[MAXN+1];
int maxti[MAXV+1][MAXG+1];

int main(){
    cin>>v>>g;
    cin>>n;
    for(int i=1;i<=n;i++) cin>>ti[i]>>vi[i]>>gi[i];

    for(int i=1;i<=n;i++)
        for(int v1=v;v1>=vi[i];v1--)
            for(int g1=g;g1>=gi[i];g1--)
                if (maxti[v1][g1]<maxti[v1-vi[i]][g1-gi[i]]+ti[i])
                        maxti[v1][g1]=maxti[v1-vi[i]][g1-gi[i]]+ti[i];
    cout<<maxti[v][g];
}
